package com.itheima.job.test05;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class UploadThread implements Runnable {
    private Socket socket ;
    public UploadThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        BufferedOutputStream bos =null;
        try {
            InputStream is = socket.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(new FileOutputStream("day15_inter\\copy\\"+ UUID.randomUUID()+".jpg"));
            int b;
            while ((b = bis.read())!=-1){
                bos.write(b);
            }
            bos.flush();
            System.out.println("上传成功");

            OutputStream os = socket.getOutputStream();
            BufferedOutputStream bos1 = new BufferedOutputStream(os);
            BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream("day15_inter\\fj.jpg"));
            int a;
            while ((a= bis1.read())!=-1){
                bos1.write(a);
            }
            socket.shutdownOutput();
            bis1.close();
            System.out.println("返回成功");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (socket!=null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos!=null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}
